import { createRouter, createWebHashHistory } from 'vue-router'
import routes from './routes'
import { useStore } from '../store/index'
import { Toast } from "vant";
import "vant/es/toast/style";
import api from '../api/index';
import pinia from '../store/store'
const store = useStore(pinia)
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach(async(to, from, next) => {
    let arr = ['/person', '/update', '/store'];
    if (arr.includes(to.path)) {
        // 校验是否登录
        let isLogin = store.isLogin
        if (isLogin) {
            next();
            return;
        }
        if (isLogin === false) {
            Toast('请您先登录哦');
            next('/login');
            return;
        }
        if (isLogin === null) {
            try {
                let { code, data } = await api.checkLogin();
                if (+code !== 0) {
                    Toast('请您先登录哦');
                    next('/login');
                    return;
                }
                store.changeIsLogin(true)
                store.changeInfo(data)
                next();
            } catch (err) {}
            return;
        }
    }
    next();
});
export default router